package com.mysiteforme.admin.controller.admin;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.mysiteforme.admin.entity.ElectricitySinglepage;
import com.mysiteforme.admin.service.ElectricitySinglepageService;
import com.baomidou.mybatisplus.plugins.Page;
import com.mysiteforme.admin.util.LayerData;
import com.mysiteforme.admin.util.RestResponse;
import com.mysiteforme.admin.annotation.SysLog;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.util.WebUtils;

import javax.servlet.ServletRequest;
import java.util.Map;

/**
 * <p>
 * 单页管理  前端控制器
 * </p>
 *
 * @author wangl
 * @since 2019-09-18
 */
@Controller
@RequestMapping("/admin/electricitySinglepage")
public class ElectricitySinglepageController {
    private static final Logger LOGGER = LoggerFactory.getLogger(ElectricitySinglepageController.class);

    @Autowired
    private ElectricitySinglepageService electricitySinglepageService;

    @GetMapping("list")
    @SysLog("跳转单页管理列表")
    public String list(){
        return "/admin/electricitySinglepage/list";
    }

    @PostMapping("list")
    @ResponseBody
    @SysLog("请求单页管理列表数据")
    public LayerData<ElectricitySinglepage> list(@RequestParam(value = "page",defaultValue = "1")Integer page,
                                      @RequestParam(value = "limit",defaultValue = "10")Integer limit,
                                      ServletRequest request){
        Map map = WebUtils.getParametersStartingWith(request, "s_");
        LayerData<ElectricitySinglepage> layerData = new LayerData<>();
        EntityWrapper<ElectricitySinglepage> wrapper = new EntityWrapper<>();
        wrapper.eq("del_flag",false);
        if(!map.isEmpty()){
            String type = (String) map.get("type");
            if(StringUtils.isNotBlank(type)) {
                wrapper.eq("type",type);
            }else{
                map.remove("type");
            }

            String content = (String) map.get("content");
            if(StringUtils.isNotBlank(content)) {
                wrapper.like("content",content);
            }else{
                map.remove("content");
            }

            String videoPath = (String) map.get("videoPath");
            if(StringUtils.isNotBlank(videoPath)) {
            }else{
                map.remove("videoPath");
            }

            String status = (String) map.get("status");
            if(StringUtils.isNotBlank(status)) {
                wrapper.eq("status",status);
            }else{
                map.remove("status");
            }
        }
        Page<ElectricitySinglepage> pageData = electricitySinglepageService.selectPage(new Page<>(page,limit),wrapper);
        layerData.setData(pageData.getRecords());
        layerData.setCount(pageData.getTotal());
        return layerData;
    }

    @GetMapping("add")
    @SysLog("跳转新增单页管理页面")
    public String add(){
        return "/admin/electricitySinglepage/add";
    }

    @PostMapping("add")
    @SysLog("保存新增单页管理数据")
    @ResponseBody
    public RestResponse add(ElectricitySinglepage electricitySinglepage){
        electricitySinglepageService.insert(electricitySinglepage);
        return RestResponse.success();
    }

    @GetMapping("edit")
    @SysLog("跳转编辑单页管理页面")
    public String edit(Long id,Model model){
        ElectricitySinglepage electricitySinglepage = electricitySinglepageService.selectById(id);
        model.addAttribute("electricitySinglepage",electricitySinglepage);
        return "/admin/electricitySinglepage/edit";
    }

    @PostMapping("edit")
    @ResponseBody
    @SysLog("保存编辑单页管理数据")
    public RestResponse edit(ElectricitySinglepage electricitySinglepage){
        if(null == electricitySinglepage.getId() || 0 == electricitySinglepage.getId()){
            return RestResponse.failure("ID不能为空");
        }
        electricitySinglepageService.updateById(electricitySinglepage);
        return RestResponse.success();
    }

    @PostMapping("delete")
    @ResponseBody
    @SysLog("删除单页管理数据")
    public RestResponse delete(@RequestParam(value = "id",required = false)Long id){
        if(null == id || 0 == id){
            return RestResponse.failure("ID不能为空");
        }
        ElectricitySinglepage electricitySinglepage = electricitySinglepageService.selectById(id);
        electricitySinglepage.setDelFlag(true);
        electricitySinglepageService.updateById(electricitySinglepage);
        return RestResponse.success();
    }

}